class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size(), left = 0, right = 0, len = 0, sum = 0, num = 0;
        for (int i = 0; i < n; i++)
        {
            num += nums[i];
        }
        if (num < x)
            return -1;
        int target = num - x;
        if (target == 0)
            return n;
        while (right < n)
        {
            sum += nums[right];
            while (sum > target)
            {
                sum -= nums[left];
                left++;
            }
            if (sum == target)
            {
                len = max(len, right - left + 1);
            }
            right++;
        }
        if (len)
            return n - len;
        return -1;

    }
};